<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="firstName">
        <el-input v-model="queryParams.firstName" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机" prop="mobilePhone">
        <el-input v-model="queryParams.mobilePhone" placeholder="请输入手机" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="身份证" prop="cardId">
        <el-input v-model="queryParams.cardId" placeholder="请输入身份证" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleArchives"
          v-hasPermi="['ehr:person:edit']">信息维护</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ehr:person:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ehr:person:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ehr:person:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ehr:person:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="personList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="员工编号" align="center" prop="partyId" />
      <el-table-column label="姓名" align="center" prop="firstName" width="120" />
      <el-table-column label="性别" align="center" prop="gender" :formatter="genderFormat" />
      <!-- <el-table-column
        label="出生日期"
        align="center"
        prop="birthDate"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="证件类型" align="center"  prop="cardType" /> -->
      <el-table-column label="身份证" align="center" prop="cardId" width="180" />
      <el-table-column label="手机" align="center" prop="mobilePhone" width="120" />
      <el-table-column label="邮箱" align="center" prop="mail" width="150" />
      <el-table-column label="最后更新时间" align="center" prop="lastUpdatedStamp" width="180">
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.lastUpdatedStamp, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="创建时间" align="center" prop="createdStamp" width="180">
    <template slot-scope="scope">
      <span>{{ parseTime(scope.row.createdStamp, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
    </template>
  </el-table-column> -->
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ehr:person:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ehr:person:remove']"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改人员信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px" :inline="true">
        <el-form-item label="姓名" prop="firstName">
          <el-input v-model="form.firstName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-input v-model="form.gender" placeholder="请输入性别" />
        </el-form-item>
        <el-form-item label="出生日期" prop="birthDate">
          <el-date-picker clearable v-model="form.birthDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="身份证" prop="cardId">
          <el-input v-model="form.cardId" placeholder="请输入身份证" />
        </el-form-item>
        <el-form-item label="证件到期日期" prop="cardDueDate">
          <el-input v-model="form.cardDueDate" placeholder="请输入证件到期日期" />
        </el-form-item>
        <el-form-item label="手机" prop="mobilePhone">
          <el-input v-model="form.mobilePhone" placeholder="请输入手机" />
        </el-form-item>
        <el-form-item label="电话" prop="telephone">
          <el-input v-model="form.telephone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="邮箱" prop="mail">
          <el-input v-model="form.mail" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="头像" prop="imageData">
          <el-input v-model="form.imageData" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="民族" prop="nation">
          <el-input v-model="form.nation" placeholder="请输入民族" />
        </el-form-item>
        <el-form-item label="籍贯" prop="nativePlace">
          <el-input v-model="form.nativePlace" placeholder="请输入籍贯" />
        </el-form-item>
        <el-form-item label="现居住地" prop="residentialAddress">
          <el-input v-model="form.residentialAddress" placeholder="请输入现居住地" />
        </el-form-item>
        <el-form-item label="家庭住址" prop="homeAddress">
          <el-input v-model="form.homeAddress" placeholder="请输入家庭住址" />
        </el-form-item>
        <el-form-item label="最高学历" prop="education">
          <el-input v-model="form.education" placeholder="请输入最高学历" />
        </el-form-item>
        <el-form-item label="健康状况" prop="health">
          <el-input v-model="form.health" placeholder="请输入健康状况" />
        </el-form-item>
        <el-form-item label="紧急联系人姓名" prop="emergencyPerson">
          <el-input v-model="form.emergencyPerson" placeholder="请输入紧急联系人姓名" />
        </el-form-item>
        <el-form-item label="紧急联系人电话" prop="emergencyPhone">
          <el-input v-model="form.emergencyPhone" placeholder="请输入紧急联系人电话" />
        </el-form-item>
        <el-form-item label="个人邮箱" prop="personalEmail">
          <el-input v-model="form.personalEmail" placeholder="请输入个人邮箱" />
        </el-form-item>
        <el-form-item label="备注" prop="comments">
          <el-input v-model="form.comments" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPerson,
  getPerson,
  delPerson,
  addPerson,
  updatePerson,
} from "@/api/ehr/person";

export default {
  name: "Person",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人员信息表格数据
      personList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 操作标识,用于区分是新增还是修改
      updateFlag: false,
      // 性别业务字典
      genderOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: "", // 排序字段
        isAsc: "", // 升序或降序 大写的 ASC或DESC,为空代表升序
        firstName: null,
        birthDate: null,
        cardId: null,
        mobilePhone: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    // 性别业务字典
    this.getEnumDicts("PERSON_GENDER").then((response) => {
      this.genderOptions = response.data;
    });
    this.getList();
  },
  methods: {
    genderFormat(row, column) {
      let val = null;
      this.genderOptions.forEach((item, index) => {
        if (row.gender === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    /** 查询人员信息列表 */
    getList() {
      this.loading = true;
      listPerson(this.queryParams).then((response) => {
        this.personList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        partyId: null,
        firstName: null,
        nickname: null,
        gender: null,
        birthDate: null,
        cardType: null,
        cardId: null,
        cardDueDate: null,
        mobilePhone: null,
        telephone: null,
        mail: null,
        imageData: null,
        nation: null,
        nativePlace: null,
        residentialAddress: null,
        homeAddress: null,
        education: null,
        politicStatus: "0",
        health: null,
        maritalStatus: "0",
        emergencyPerson: null,
        emergencyPhone: null,
        personalEmail: null,
        comments: null,
        totalYearsWorkExperience: null,
        yearsWithEmployer: null,
        monthsWithEmployer: null,
        salutation: null,
        middleName: null,
        lastName: null,
        personalTitle: null,
        suffix: null,
        firstNameLocal: null,
        middleNameLocal: null,
        lastNameLocal: null,
        otherLocal: null,
        memberId: null,
        deceasedDate: null,
        height: null,
        weight: null,
        mothersMaidenName: null,
        socialSecurityNumber: null,
        passportNumber: null,
        passportExpireDate: null,
        occupation: null,
        employmentStatusEnumId: null,
        residenceStatusEnumId: null,
        existingCustomer: null,
        lastUpdatedStamp: null,
        lastUpdatedTxStamp: null,
        createdStamp: null,
        createdTxStamp: null,
        cmbcCardId: null,
        cardNumbers: null,
        bankOfDeposit: null,
        bankingOutlets: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.partyId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 用户档案信息
    handleArchives(row) {
      this.updateFlag = true;
      console.info("row:" + JSON.stringify(row));
      const partyId = row.partyId || this.ids[0];
      var _path = "/user/archives";
      this.$router.push({
        path: _path,
        query: {
          partyId: partyId,
        },
      });
      // getPerson(partyId).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "员工档案信息";
      // });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.updateFlag = false;
      this.reset();
      this.open = true;
      this.title = "添加人员信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.updateFlag = true;
      this.reset();
      const partyId = row.partyId || this.ids;
      getPerson(partyId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改人员信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.partyId != null && this.updateFlag) {
            updatePerson(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPerson(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const partyIds = row.partyId || this.ids;
      this.$modal
        .confirm('是否确认删除人员信息编号为"' + partyIds + '"的数据项？')
        .then(function () {
          return delPerson(partyIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "ehr/person/export",
        {
          ...this.queryParams,
        },
        `person_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
